/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.model.base;


/**
 * Utility methods for dealing with Dex identifiers.
 */
public class Identifiers {

	
	/**
	 * Static utility library.
	 */
	private Identifiers() {
	}
	
	
	/**
	 * Returns true if the given identifier is qualified with namespace information.
	 * 
	 * @param id  the identifier to check
	 * @return    true if that identifier is qualified with namespace information
	 */
	public static boolean isQualified(String id) {
		return id.indexOf(':') > 0;
	}
	
	
	/**
	 * Returns true if the given identifier is valid.
	 * 
	 * @param id  the identifier to check
	 * @return    true if that identifier is valid
	 */
	public static boolean isValid(String id) {
		if (!isQualified(id)) {
			return isValidPart(id);
		}
		String[] parts = id.split(":");
		for (String part: parts) {
			if (!isValidPart(part)) {
				return false;
			}
		}
		return true;
	}
	
	
	/**
	 * Checks that an unqualified part of an identifier is valid.
	 *
	 * @param part  the identifier part to check
	 * @return      true if that part is valid
	 */
	private static boolean isValidPart(String part) {
		if (part.length() <= 0) {
			return false;
		}
		char first = part.charAt(0);
		if (!Character.isLetter(first)) {
			return false;
		}
		for (int i = 1; i < part.length(); i++) {
			if (!Character.isJavaIdentifierPart(part.charAt(i))) {
				return false;
			}
		}
		return true;
	}
	
	
	/**
	 * Returns the unit designator of a qualified identifier.
	 * 
	 * @param id  the identifier to check
	 * @return    the unit designator
	 * @throws IllegalArgumentException  if the identifier is not fully qualified
	 */
	public static String getUnit(String id) {
		if (!isValid(id)) {
			throw new IllegalArgumentException("Invalid identifier.");
		}
		if (!isQualified(id)) {
			throw new IllegalArgumentException("Can't derive unit from unqualified identifier.");
		}
		int p = id.lastIndexOf(':');
		return id.substring(0, p);
	}
	
	
	/**
	 * Returns the base name of a qualified identifier.
	 * 
	 * @param id  the identifier to check
	 * @return    the base name
	 * @throws IllegalArgumentException  if the identifier is not fully qualified
	 */
	public static String getBase(String id) {
		if (!isValid(id)) {
			throw new IllegalArgumentException("Invalid identifier.");
		}
		if (!isQualified(id)) {
			throw new IllegalArgumentException("Can't derive base from unqualified identifier.");
		}
		int p = id.lastIndexOf(':');
		return id.substring(p + 1);
	}
	
}

